import types from '../types'
import common from '../common'
import store from '../index.js'
import axios from 'axios'
import moment from 'moment'
import env from '@/config/env'
import {apiGetSign} from '@/service/apiService'
import {apiGetUserCoupon} from '@/service/apiSvcCoupon'
import {custsign, custsign2, createSignData} from '@/myutl/utlenc'

const state = {
  coupons: null,        // 用户优惠信息(列表)
  nearbyMchts: null,    // 附近商户信息(列表)
  hasCoupon: false,
  couponErrmsg: null
}

const getters = {
  coupons(state) {
    return state.coupons;
  },
  nearbyMchts(state) {
    return state.couponMchts;
  },
  hasCoupon(state) {
    return state.hasCoupon;
  },
  couponErrmsg(state) {
    return state.couponErrmsg;
  }
}

const actions = {
  async getCoupons({commit, state}, userId) {
    let curTs = moment().format('YYYYMMDDHHmmss');
    let params = {
      "appId": env.appId,
      "charset": "utf-8",
      "timestamp": curTs,
      "version": "1.0",
      "format": "JSON",
      "appAuthToken": store.getters.authToken,
      "bizContent": {
        "userId": userId
      }
    };
    let signData = createSignData(params);
    let retData = await apiGetSign(signData);
    if (retData.errno != 0) {
      commit(types.LOGIN_USER, {isLoginSucc: false, errmsg: "登录超时请重试"});
      return;
    }
    params.signType = "RSA2";
    params.sign = retData.sign;
    console.log("params:", params);

    apiGetUserCoupon(params).then((rsp) => {
      console.log("apiGetUserCoupon-then:", rsp.data);
      if (rsp.data.subCode === undefined  || rsp.data.subCode === null) {
        commit(types.GET_COUPONS, {isRetSucc:false, errmsg:rsp.data.msg});
      } else {
        if (rsp.data.subCode !== "0000") {
          commit(types.GET_COUPONS, {isRetSucc:false, errmsg:rsp.data.subMsg});
        } else {
          commit(types.GET_COUPONS, {isRetSucc:true, coupons:rsp.data.detail});
        }
      } 
    });
  },
  async getNearbyMchts({commit, state}, postionInf) {
    let curTs = moment().format('YYYYMMDDHHmmss');
    let params = {
                  "appId": appId,
                  "charset": "utf-8",
                  "timestamp": curTs,
                  "version": "1.0",
                  "format": "JSON",
                  "appAuthToken": store.getters.authToken,
                  "bizContent": {
                    "addressLng": postionInf.lng,
                    "addressLat": postionInf.lat
                  }
                };
    let signData = createSignData(params);
    let retData = await apiGetSign(signData);
    if (retData.errno != 0) {
      commit(types.ACTION_RET, {isLoginSucc: false, errmsg: "登录超时请重试"});
      return;
    }
    params.signType = "RSA2";
    params.sign = retData.sign;
    console.log("params:", params);

    apiAsyncSnd2Coupon(params).then((rsp) => {
      console.log("apiAsyncSnd2Coupon-then:", rsp.data);
      if (rsp.data.subCode === undefined  || rsp.data.subCode === null) {
        commit(types.GET_NEARBYMCHTS, {isRetSucc:false, errmsg:rsp.data.msg});
      } else {
        if (rsp.data.subCode !== "0000") {
          commit(types.GET_NEARBYMCHTS, {isRetSucc:false, errmsg:rsp.data.subMsg});
        } else {
          commit(types.GET_NEARBYMCHTS, {isRetSucc:true, listInf:rsp.data.detail});
        }
      } 
    });
  }
}

const mutations = {
  [types.GET_COUPONS](state, data){
    if (data.isRetSucc) {
      state.coupons = data.coupons;
      state.hasCoupon = true;
    } else {
      state.hasCoupon = false;
      state.couponErrmsg = data.errmsg;
    }
  },
  [types.GET_NEARBYMCHTS](state, data){
    if (data.isRetSucc) {
      state.nearbyMchts = data.listInf;
    } else {
      state.couponErrmsg = data.errmsg;
    }
  },
}

export default {
  state,
  getters,
  actions,
  mutations
}
